#include "consignee.h"
#include "ui_consignee.h"

Consignee::Consignee(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Consignee)
{
    ui->setupUi(this);
    ui->pushButton->setDisabled(true);
    this->setFixedSize(440,720);
    socket = SocketManager::getinstance().socket();
    connect(socket,&QAbstractSocket::readyRead,this,&Consignee::rcv_rig);
}

Consignee::~Consignee()
{
    delete ui;
}

void Consignee::on_pushButton_2_clicked()
{
    emit(returnreg());
}
//用户条例判断
void Consignee::on_checkBox_stateChanged(int arg1)
{
    if(arg1 == 0)
    {
        ui->pushButton->setDisabled(true);
    }
    else
    {
        ui->pushButton->setDisabled(false);
    }
}

//用户注册
void Consignee::on_pushButton_clicked()
{
    sendRig srg;
    const char* account = ui->lineEdit->text().toUtf8().constData();
    strcpy(srg.Account,account);
    const char* password = ui->lineEdit_2->text().toUtf8().constData();
    strcpy(srg.password,password);
    const char* telenum = ui->lineEdit_3->text().toUtf8().constData();
    strcpy(srg.tele,telenum);
    srg.type=1;
    srg.IC=0;
    srg.Idtype = true;
    QByteArray data;
    data.append((char*)&srg,sizeof (sendRig));
    socket->write(data);
}

void Consignee::rcv_rig()
{
    if(!this->isActiveWindow()){
        return;
    }
    sendRig rd;
    while (socket->bytesAvailable() >= sizeof(sendRig)) {
            socket->read(reinterpret_cast<char*>(&rd), sizeof(sendRig));
    }
//    rd = (sendRig *)socket->readAll().data();
    if(rd.type == 1){
        if(rd.IC == 1)
            {
                emit(cee_rigfinish());
            }else if(rd.IC == -1)
         {
             //电话号码已注册
             QMessageBox::warning(this,"注册失败","电话号码已注册","返回 ");
         }else{
                 //注册失败
            QMessageBox::warning(this,"注册失败","注册失败，请稍后重试","返回 ");
        }
    }
}
